package com.puris.dao.admin;

import com.puris.dao.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Sonata
 * @data 2021/11/28 17:40
 */
public class FindCompetitionDaoImpl implements FindCompetitionDao {
    @Override
    public StringBuffer findCompetition(Connection connection, String id) throws SQLException {
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        StringBuffer competition = new StringBuffer();

        if (connection != null) {
            String sql = "select * from competition where id = ?";
            Object[] params = {id};

            resultSet = BaseDao.execute(connection, statement, resultSet, sql, params);

            if (resultSet != null) {
                competition.append("<table>");
                competition.append("<tr>");
                competition.append("<th>编号</th>");
                competition.append("<th>比赛</th>");
                competition.append("<th>日期</th>");
                competition.append("<th>描述</th>");
                competition.append("<th>工号</th>");
                competition.append("<th>教师</th>");
                competition.append("</tr>");
                while (resultSet.next()) {
                    competition.append("<tr>");
                    competition.append("<td>").append(resultSet.getString(1)).append("</td>");
                    competition.append("<td>").append(resultSet.getString(2)).append("</td>");
                    competition.append("<td>").append(resultSet.getString(3)).append("</td>");
                    competition.append("<td>").append(resultSet.getString(4)).append("</td>");
                    competition.append("<td>").append(resultSet.getString(5)).append("</td>");
                    competition.append("<td>").append(resultSet.getString(6)).append("</td>");
                    competition.append("</tr>");
                }
                competition.append("</table>");
                competition.append("<form method=\"post\" name=\"updateCompetition\" action=\"/SchoolCompetition_war/updateCompetition.do\">\n" +
                        "    <p>\n" +
                        "        编号：\n" +
                        "        <label>\n" +
                        "            <input type=\"number\" name=\"id\" value=\"" + id + "\">\n" +
                        "        </label>\n" +
                        "    </p>\n" +
                        "    <p>\n" +
                        "        名称：\n" +
                        "        <label>\n" +
                        "            <input type=\"text\" name=\"competitionName\">\n" +
                        "        </label>\n" +
                        "    </p>\n" +
                        "    <p>\n" +
                        "        日期：\n" +
                        "        <label>\n" +
                        "            <input type=\"date\" name=\"date\">\n" +
                        "        </label>\n" +
                        "    </p>\n" +
                        "    <p>\n" +
                        "        描述：\n" +
                        "        <label>\n" +
                        "            <textarea name=\"describe\"></textarea>\n" +
                        "        </label>\n" +
                        "    </p>\n" +
                        "    <p>\n" +
                        "        工号：\n" +
                        "        <label>\n" +
                        "            <input type=\"number\" name=\"teacherId\">\n" +
                        "        </label>\n" +
                        "    </p>\n" +
                        "    <p>\n" +
                        "        教师：\n" +
                        "        <label>\n" +
                        "            <input type=\"text\" name=\"teacherName\">\n" +
                        "        </label>\n" +
                        "    </p>\n" +
                        "    <input type=\"submit\" value=\"添加\">\n" +
                        "</form>");
            }
        }
        BaseDao.closeResource(null, statement, resultSet);

        return competition;
    }
}
